#ifndef _FXMATH_NET_
#define _FXMATH_NET_

#if (_FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || _FX_OS_ == _FX_WIN64_)
	#if _FX_OS_ != _FX_WIN32_MOBILE_
		#include <winsock.h>
	#else
		#include <winsock2.h>
	#endif
	#ifdef ParseURL
		#undef ParseURL
	#endif
#else //linux
	#include <errno.h>
	#include <netdb.h>
	#include <unistd.h>
	#include <sys/types.h>
	#include <sys/socket.h>
	#include <sys/ioctl.h>
	#if (_FX_OS_ == _FX_ANDROID_)
		#include <fcntl.h>
	# else
		#include <sys/fcntl.h>
	#endif
	#include <netinet/in.h>
	#include <arpa/inet.h>
#endif

//*****************************************************************************
//* Network module management & utilities
//*****************************************************************************
const void*		FX_Network_InitModule();
void			FX_Network_ReleaseModule(const void* handle);

CFX_ByteString	FX_Network_GetHostName(FX_BSTR str);
CFX_ByteString	FX_Network_GetIPV4Address(FX_BSTR str);
FX_INT32		FX_Network_GetProtocalPort(FX_BSTR str);

//*****************************************************************************
//* URL
//*****************************************************************************
class CFX_URL
{
public:
	CFX_URL();
	~CFX_URL();

	static void		DecodeURL(CFX_ByteString &dst, FX_BSTR src);
	static void		EncodeURL(CFX_ByteString &dst, FX_BSTR src);
	static void		ParseURL(FX_BSTR urlStr, CFX_URL &urlInfo, FX_BOOL bDecode = FALSE);

	void			Reset();

	CFX_ByteString	m_csProtocal;
	CFX_ByteString	m_csUser;
	CFX_ByteString	m_csPassword;
	CFX_ByteString	m_csHost;
	CFX_ByteString	m_csPort;
	CFX_ByteString	m_csObject;
};

//*****************************************************************************
//* Socket
//*****************************************************************************
class IFX_Socket
{
public:
	virtual void		Release() = 0;

	virtual void		SetTimeout(FX_INT32 nSeconds) = 0;
	virtual FX_BOOL		Connect(FX_BSTR szHost, FX_BSTR szProtocal) = 0;
	virtual FX_BOOL		Connect(FX_BSTR szHost, FX_INT32 nPort) = 0;
	virtual FX_BOOL		Connect(const CFX_URL &url) = 0;
	virtual FX_INT32	SendData(FX_BSTR szData) = 0;
	virtual FX_INT32	ReceiveData(FX_LPBYTE pBuffer, FX_INT32 nLen) const = 0;
	virtual void		Close() = 0;
};

IFX_Socket*		FX_Socket_Create(FX_BOOL bUDP = FALSE, FX_BOOL bNonblock = TRUE);

#endif //_FXMATH_NET_
